/**
 * @file usr_board.c
 * @author zcj (605607217@qq.com)
 * @brief
 * @version ecu-中控
 * @date 2023-11-06
 *
 * @copyright Copyright (c) 2023
 *
 */

#include "usr_board.h"


/**
 * @brief
 *
 * @param sec
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
void usr_delay_s(uint32_t sec)
{
    luat_rtos_task_sleep(sec * 1000);
}

/**
 * @brief
 *
 * @param msec
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
void usr_delay_ms(uint32_t msec)
{
    luat_rtos_task_sleep(msec);
}

/**
 * @brief
 *
 * @param pin_num
 * @param output
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
void gpio_output_init(uint32_t pin_num, uint8_t output)
{
    luat_gpio_cfg_t gpio_cfg;
    luat_gpio_set_default_cfg(&gpio_cfg);
    gpio_cfg.pin = pin_num;
    gpio_cfg.pull=Luat_GPIO_PULLDOWN;
    gpio_cfg.mode=Luat_GPIO_OUTPUT;
    gpio_cfg.output_level=Luat_GPIO_LOW;
    luat_gpio_open(&gpio_cfg);
    luat_gpio_set(pin_num, output);
}

/**
 * @brief
 *
 * @param pin_num
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
void gpio_input_init(uint32_t pin_num)
{
    luat_gpio_cfg_t gpio_cfg;
    luat_gpio_set_default_cfg(&gpio_cfg);

    gpio_cfg.pin = pin_num;
    gpio_cfg.mode = LUAT_GPIO_INPUT;
    luat_gpio_open(&gpio_cfg);
}

/**
 * @brief
 *
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
void gpio_dev_init(void)
{
    gpio_output_init(GNSS_STATUS_PIN, Luat_GPIO_LOW);
    gpio_output_init(ACC72_SW_PIN, Luat_GPIO_LOW);
    gpio_output_init(LOCK_CTRL_PIN, Luat_GPIO_LOW);
    gpio_output_init(NET_STATUS_PIN, Luat_GPIO_LOW);
    gpio_output_init(BEEP_PIN, Luat_GPIO_LOW);//不响
    gpio_input_init(WHEEL_STATE_PIN);

}

/**
 * @brief
 *
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
void adc_dev_init(void)
{
    luat_adc_ctrl_param_t ctrl_param;
    ctrl_param.range = LUAT_ADC_AIO_RANGE_1_2;
    luat_adc_ctrl(BAT_DET_CHECK, LUAT_ADC_SET_GLOBAL_RANGE, ctrl_param);
    luat_adc_open(BAT_DET_CHECK, NULL);
    luat_adc_open(LUAT_ADC_CH_CPU, NULL);
    luat_adc_open(LUAT_ADC_CH_VBAT, NULL);
}
// void gpio_level_irq(void *data, void* args)
// {
// 	int pin = (int)data;
// 	LUAT_DEBUG_PRINT("pin:%d, level:%d,", pin, luat_gpio_get(pin));
// }

/**********************wakeup 中断功能演示*********Error: ziped data size > filesize - flashXIPLimit, 102400, 1253376, 1168424 Error: Encounter error a*********************/
//wakeup 引脚一般作为中断引脚或者休眠唤醒引脚
// void wakeup0_init(void)
// {
//     luat_gpio_cfg_t wakeup0_struct;
//     wakeup0_struct.pin=HAL_WAKEUP_0;
//     wakeup0_struct.pull=Luat_GPIO_PULLUP;
//     wakeup0_struct.mode=Luat_GPIO_IRQ;
//     wakeup0_struct.irq_type=LUAT_GPIO_FALLING_IRQ;
//     wakeup0_struct.irq_cb=(void*)gpio_level_irq;
//     luat_gpio_open(&wakeup0_struct);
// }
/**
 * @brief
 *
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
void hard_ware_init(void)
{
    gpio_dev_init();
    adc_dev_init();
    //
}

/**
 * @brief
 *
 * @param sw
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
void v_acc_ctrl(uint8_t sw)
{
    luat_gpio_set(ACC72_SW_PIN, sw);
    if (sw == true)
    {
        g_ebike_status.acc_statu = V_ON;
    }
    else
        g_ebike_status.acc_statu = V_OFF;
    
}

/**
 * @brief
 *
 * @param sw
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
void v_lock_ctrl(uint8_t sw)
{
    luat_gpio_set(LOCK_CTRL_PIN, sw);
    if (sw == true)
    {
        g_ebike_status.bike_locked = V_ON;
    }
    else
        g_ebike_status.bike_locked = V_OFF;
}
void v_beep_ctrl(uint8_t sw)
{
    luat_gpio_set(BEEP_PIN, sw);
}
/**
 * @brief
 *
 * @return uint8_t
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
uint8_t wheel_status_check(void)
{
    return luat_gpio_get(WHEEL_STATE_PIN);
}

/**
 * @brief
 *
 * @param sw
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
void gnss_led_ctrl(uint8_t sw)
{
    luat_gpio_set(GNSS_STATUS_PIN, sw);
}

/**
 * @brief
 *
 * @param sw
 * @author zcj (605607217@qq.com)
 * @date 2023-11-07
 */
void net_led_ctrl(uint8_t sw)
{
    luat_gpio_set(NET_STATUS_PIN, sw);
}

